package com.android.twitter;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class Parser {
	ArrayList<Status> list = new ArrayList<Status>();
	Boolean flag;

	ArrayList<Status> parse(InputStream iss) {
		try{
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser xpp = factory.newPullParser();
			InputStream is = iss;
			InputStreamReader reader = new InputStreamReader(is);
			xpp.setInput(reader);
			list = parseStatus(xpp);			
			return list;			
		}catch(XmlPullParserException e) {
			Log.e("TA", "Parser.java , Parse Error = " + e.getMessage());
		} catch(Exception e){
			Log.e("TA", "Parser.java , IO Error = " + e.getMessage());
		}	
		return list;		
	}

	ArrayList<Status> parseStatus(XmlPullParser xpp) throws IOException, XmlPullParserException{		
		Status st = new Status();
		Status gar = new Status();
		flag=true;
		
		while(true){
			int eventType = xpp.nextTag();
			if(eventType == XmlPullParser.START_TAG) {
				String tag = xpp.getName();
				if("status".equals(tag)){					
					st = new Status();
				}else if("id".equals(tag)){
					st.id =xpp.nextText();
					Log.d("ID", "id : " + st.id);
					Log.d("TA", st.id);
				}else if("created_at".equals(tag)){
					st.create_at = xpp.nextText();
				}else if("text".equals(tag)){
					st.text = xpp.nextText();
				}else if("user".equals(tag) && flag){
					st.user = parseUser(xpp);					
				}else if("user".equals(tag) && !flag){
					gar.user = parseUser(xpp);					
					flag=!flag;
				}else if("statuses".equals(tag)){
					xpp.nextTag();
				}else if("geo".equals(tag)){					
					if(!xpp.isEmptyElementTag()){
						xpp.nextTag();
						xpp.nextText();
						xpp.nextTag();
					}else {
						xpp.nextText();
					}
				}else if("coordinates".equals(tag)){
					//Log.d("TA","coordinates");
					if(!xpp.isEmptyElementTag()){
						xpp.nextTag();
						xpp.nextText();
						xpp.nextTag();
					}else {
						xpp.nextText();
					}
				}else if("retweeted_status".equals(tag)){					
					abc(xpp, st);
//					Log.d("TA", "else");					
//					Log.d("TA", "1");
					flag = false;
//					Log.d("TA", "2");
				}								
				else{
					xpp.nextText();
					flag = true;
				}
			}			
			else if (eventType == XmlPullParser.END_TAG && (xpp.getName()).equals("status")){				
				list.add(st);
				
			}else {
				break;
			}				
		}
		return list;
	}
	User parseUser(XmlPullParser xpp) throws IOException, XmlPullParserException {		
		User user = new User();
		
		while(true){
			int eventType = xpp.nextTag();
			if(eventType == XmlPullParser.START_TAG) {
				String tag = xpp.getName();
				if("user".equals(tag)){
					
				}else if("screen_name".equals(tag)){
					user.screen_name = xpp.nextText();
//					Log.d("Twitter2", "User screen_name : " + user.screen_name );
					
				}else if("profile_image_url".equals(tag)){
					user.profile_image_url = xpp.nextText();
//					Log.d("Twitter2", "User profile_image_url : " + user.profile_image_url );
				}else{
					xpp.nextText();
				}
			}else if (eventType == XmlPullParser.END_TAG && (xpp.getName()).equals("status")){				
					break;
			}else{
				break;
			}
				
		}
		return user;
	}
	
	public User parseUser2(InputStream iss) throws IOException, XmlPullParserException {
		
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		XmlPullParser xpp = factory.newPullParser();		
				
		InputStream is = iss;
		InputStreamReader reader = new InputStreamReader(is);
		xpp.setInput(reader);
		
		Log.d("Twitter2", "parseUser2 - 1 = " + xpp.getName());			
		
		User user = new User();
				
		while(true){			
			String tag = xpp.getName();			
			Log.d("Twitter2", "parseUser2 - 4 = " + tag);		
			int eventType = xpp.nextTag();			
			if(eventType == XmlPullParser.START_TAG) {
				Log.d("Twitter2", "parseUser2 - 7 = " + tag);
				
				if("id".equals(tag)){
					Log.d("Twitter2", "User =" + tag);
					xpp.nextText();
				}else if("screen_name".equals(tag)){
					user.screen_name = xpp.nextText();
					Log.d("Twitter2", "screen_name =" + user.screen_name);		
					
				}else if("profile_image_url".equals(tag)){
					user.profile_image_url = xpp.nextText();
					Log.d("Twitter2", "profile_image_url =" + user.profile_image_url);
					
				}else if("user".equals(tag)){
					Log.d("Twitter2", "user? " + tag);
					
				}else if(eventType == XmlPullParser.START_TAG && tag.equals("status")){					
					Log.d("Twitter2", "Status = " + tag);
					parseStatus(xpp);	
					
				}else{
					Log.d("Twitter2", "First else = " + tag);
					xpp.nextText();
				}
			}else{
				break;
			}
			return user;
		}
		return user;
	}
	
	void abc(XmlPullParser xpp, Status stt) throws IOException, XmlPullParserException{
		Log.d("TA" , "1" + xpp.getName());
		while(true){
			Log.d("TA" , "2" + xpp.getName());
			int eventType = xpp.nextTag();
			Log.d("TA" , "5" + xpp.getName());
			if(eventType == XmlPullParser.START_TAG) {
				 String tag = xpp.getName();
				if("created_at".equals(tag)){
					stt.create_at = xpp.nextText();
					Log.d("TA", "CR" + stt.create_at);
				}else if("text".equals(tag)){
					stt.text = xpp.nextText();
					stt.text = stt.text + " \n\n Retwwt";
					Log.d("TA", "text " + stt.text);
				}else if("user".equals(tag)){
					stt.user = parseUser(xpp);
					Log.d("TA", "user" + stt.user.screen_name + " userimg " + stt.user.profile_image_url);
				}else if("statuses".equals(tag)){
					xpp.nextTag();
				}else if("geo".equals(tag)){
					//Log.d("TA","geo");
					if(!xpp.isEmptyElementTag()){
						xpp.nextTag();
						xpp.nextText();
						xpp.nextTag();
					}else {
						xpp.nextText();
					}
				}else if("coordinates".equals(tag)){
					//Log.d("TA","coordinates");
					if(!xpp.isEmptyElementTag()){
						xpp.nextTag();
						xpp.nextText();
						xpp.nextTag();
					}else {
						xpp.nextText();
					}
				}else{
					Log.d("TA", "Others Name = " + xpp.getName());
					xpp.nextText();
				}
				
			}else if(eventType == XmlPullParser.END_TAG && "retweeted_status".equals(xpp.getName())){
				Log.d("TA", "Last TaG = " + xpp.getName());
				break;
			}			
		}
	}	
}